package com.achen.elasticsearch.dao.impl;

import com.achen.elasticsearch.dao.UserRepository;
import com.achen.entity.User;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author achen
 * @create 2020-05-04 17:44
 */
@Repository
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public List<User> findBySearch(String querykeyword) {
        List<User> list = null;
        if(querykeyword == null || querykeyword == ""){
            SearchQuery searchQuery = new NativeSearchQuery(QueryBuilders.matchAllQuery());
            list = elasticsearchTemplate.queryForList(searchQuery, User.class);
        }else{
            SearchQuery searchQuery = new NativeSearchQuery(QueryBuilders.termQuery("name", querykeyword));
            list = elasticsearchTemplate.queryForList(searchQuery, User.class);

        }
        return list;
    }
}
